<?php
/**
 * banner
 */
namespace app\admin\controller;
use controller\BasicAdmin;
use think\Db;
class Adminbanner extends BasicAdmin{
    public $table = 'banner';
    public $banner_position = 'banner_position';

    public function index()
    {
        list($get, $db) = [$this->request->get(), Db::name($this->table)->alias('a')->join('t_banner_position b','a.pid=b.id')
            ->join('t_picture c','a.pictureid=c.id','LEFT')
        ->field('a.id,a.name,a.imgHref,a.hrefType,a.status,a.addtime,a.starttime,a.endtime,a.pid,b.name as pname,c.picurl')];

        isset($get['tel']) &&$get['tel'] !='' ?$where ='tel='.trim($get['tel']) :$where = '1=1';
        $list = parent::_list($db->where($where)->order('a.addtime DESC'),1,'','');
        foreach ($list['list'] as $k=>$v){
           $list['list'][$k]['purl'] = $this->imgUrl($v['picurl']);
        }
        return $this->fetch('', ['title' => 'banner列表','list'=>$list]);
    }

    public function edit()
    {
        list($get,$db) = [$this->request->get(),Db::name($this->table)->alias('a')->join('t_picture b','a.pictureid=b.id','LEFT')
        ->field('a.*,b.picurl')];
        $vo = $db->where(['a.id'=>$get['id']])->find();
        $vo['purl'] = $this->imgUrl($vo['picurl']);
        $vo['list'] = Db::name($this->banner_position)->where(['status'=>1])->select();
        return $this->fetch('form', ['title' => '广告修改','vo'=>$vo]);
    }
    public function editpost()
    {
        list($post, $db) = [$this->request->post(), Db::name($this->table)];
        $data['name'] = $post['name'];
        $data['imgHref'] = $post['imgHref'];
        $data['status'] = $post['status'];
        $data['pid'] = $post['pid'];
        $data['starttime'] = strtotime($post['starttime']);
        $data['endtime'] = strtotime($post['endtime']);
        $data['sort'] = $post['sort'];
        $data['pictureid'] = $post['pictureid'];
        $result = $db->where(['id'=>$post['id']])->update($data);
        if(!empty($result)){
            $this->success('操作成功','');
        }else{
            $this->error('操作失败','');
        }
    }

    /**
     * 添加广告
     */

    public function add(){
     return $this->fetch('', ['title' => '添加广告']);
    }
    public function addbanner(){
        list($post, $db) = [$this->request->post(), Db::name($this->table)];
        $insert['name'] = $post['name'];
        $insert['pictureid'] = $post['img'];
        $insert['imgHref'] = $post['imgHref'];
        $insert['sort'] = $post['sort'];
        $insert['pid'] = 1;
        $insert['starttime'] = strtotime($post['starttime']);
        $insert['endtime'] = strtotime($post['endtime']);
        $insert['addtime'] = time();
        $insert['status'] = 1;

        $restule = $db->insert($insert);
        if(!empty($restule)){
            $this->success('新增成功','');
        }else{
            $this->error('新增失败','');
        }

    }

}